package com.mmd.manage.web;

import com.mmd.bean.CODE;
import com.mmd.bean.UserLoginContext;
import com.mmd.bean.WebJsonBean;
import com.mmd.common.entity.MmdQuotationDetail;
import com.mmd.intercepter.AdminInterceptor;
import com.mmd.manage.service.QuotationService;
import com.mmd.manage.vo.QuotationEo;
import com.mmd.manage.vo.QuotationVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * 报价单
 */
@RequestMapping(value = "/api/quotation")
@Controller
public class QuotationController {

	@Autowired
	private QuotationService quotationService;


	@RequestMapping(value = "/addQuotation")
	@ResponseBody
	public WebJsonBean addRoute(QuotationEo eo) {
		return new WebJsonBean(CODE.SUCCESS,quotationService.addQquotation(eo));
	}

	@RequestMapping(value = "/modifyQuotation")
	@ResponseBody
	public WebJsonBean modifyQuotation(QuotationEo eo) {
		return new WebJsonBean(CODE.SUCCESS,quotationService.modifyQuotation(eo));
	}

	/**
	 * 查找渠道路线列表
	 * @return
	 */
	@RequestMapping(value = "/getRouteNameList")
	@ResponseBody
	public WebJsonBean getRouteNameList( String channelNo, Long areaId){
		String level = null;
		UserLoginContext userLoginContext = AdminInterceptor.getUserLoginContext();
		if ("user".equals(userLoginContext.getLevel())) {
			//用户查询时才有用
			level = userLoginContext.getUserLevel();
		}
		if (StringUtils.isBlank(userLoginContext.getUserLevel()) && "user".equals(userLoginContext.getLevel())) {
			//如果没有级别，默认查D级别
			level = "D";
		}
		return new WebJsonBean(CODE.SUCCESS, quotationService.getRouteNameList(level,channelNo,areaId));
	}

	/**
	 * 查找渠道路线的有效时间列表
	 * @return
	 */
	@RequestMapping(value = "/getValidTimeByRouteName")
	@ResponseBody
	public WebJsonBean getValidTimeByRouteName(String routeNo, String channelNo, Long areaId){
		return new WebJsonBean(CODE.SUCCESS, quotationService.getValidTimeByRouteName(routeNo, channelNo, areaId));
	}

	/**
	 * 用户获取报价单
	 * @return
	 */
	@RequestMapping(value = "/userGetQuotation")
	@ResponseBody
	public WebJsonBean userGetQuotation(String quotationNo){
		return new WebJsonBean(CODE.SUCCESS, quotationService.userGetQuotation(quotationNo));
	}

	@RequestMapping(value = "/getQuotationDetail")
	@ResponseBody
	public WebJsonBean getQuotationDetail(String quotationNo){
		return new WebJsonBean(CODE.SUCCESS, quotationService.getQuotationDetail(quotationNo));
	}

	@RequestMapping(value="/export")
	@ResponseBody
	public WebJsonBean export(HttpServletRequest request, HttpServletResponse response){
		HSSFWorkbook wb = new HSSFWorkbook();

		String routeName = "";
		//获取所有报价单
		List<QuotationVo> quotationList = quotationService.getQuotationList();
		// 填充数据
		for (int j = 1; j <= quotationList.size(); j++) {
			QuotationVo vo = quotationList.get(j-1);
			routeName = vo.getRouteName();
			// sheet 对应一个工作页
			Sheet sheet = wb.createSheet(vo.getRouteName());
			sheet.setDefaultColumnWidth(10);
			sheet.setDefaultRowHeightInPoints(20);
			// 创建第一行为标题行
			Row hssfRow = sheet.createRow(0);
			Cell hssfCell = hssfRow.createCell(0);
			String[] titleArr = {"路线名称","渠道","区域","21kg+","46kg+","71kg","101kg","301kg","501kg","1001kg","提取时间","单票操作费","单票报关费","中港"};
			int i = 0;
			/* for循环生成列名 */
			for (String key : titleArr) {
				hssfCell = hssfRow.createCell(i);
				hssfCell.setCellValue(key);
				i++;
			}

			List<MmdQuotationDetail> detailList = vo.getQuotationDetails();
			for (int x = 1; x <= detailList.size(); x++) {
				MmdQuotationDetail detail = detailList.get(x-1);
				hssfRow = sheet.createRow(x);

				//路线名称
				hssfCell = hssfRow.createCell(0);
				hssfCell.setCellValue(vo.getRouteName());
				//模式
				hssfCell = hssfRow.createCell(1);
				hssfCell.setCellValue(vo.getChannelName());
				//区域
				hssfCell = hssfRow.createCell(2);
				hssfCell.setCellValue(detail.getSonAreaName());
				//21kg+
				hssfCell = hssfRow.createCell(3);
				hssfCell.setCellValue(detail.getKg21().toString());
				//46kg+
				hssfCell = hssfRow.createCell(4);
				hssfCell.setCellValue(detail.getKg46().toString());
				//71kg
				hssfCell = hssfRow.createCell(5);
				hssfCell.setCellValue(detail.getKg71().toString());
				//101kg
				hssfCell = hssfRow.createCell(6);
				hssfCell.setCellValue(detail.getKg101().toString());
				//301kg
				hssfCell = hssfRow.createCell(7);
				hssfCell.setCellValue(detail.getKg301().toString());
				//501kg
				hssfCell = hssfRow.createCell(8);
				hssfCell.setCellValue(detail.getKg501().toString());
				//1001kg
				hssfCell = hssfRow.createCell(9);
				hssfCell.setCellValue(detail.getKg1001().toString());
				//提取时间
				hssfCell = hssfRow.createCell(10);
				hssfCell.setCellValue(detail.getDrawTime());
				//单票操作费
				hssfCell = hssfRow.createCell(11);
				hssfCell.setCellValue(vo.getSingleOperatFee().toString());
				//单票报关费
				hssfCell = hssfRow.createCell(12);
				hssfCell.setCellValue(vo.getSingleDeclareFee().toString());
				//中港
				hssfCell = hssfRow.createCell(13);
				hssfCell.setCellValue(vo.getZgFee().toString());
			}
		}

		// 创建文件输出流，准备输出电子表格
		try {
			String fileName = routeName + "路线-"+System.currentTimeMillis()+".xls";
			String agent = request.getHeader("User-Agent");
			boolean isMSIE = (agent != null && agent.indexOf("MSIE") != -1);
			if( isMSIE ){
				fileName = java.net.URLEncoder.encode(fileName,"UTF8");
			}else{
				//兼容火狐，否则中文乱码
				fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
			}
			response.reset();
			response.setContentType("application/vnd.ms-excel");
			response.setCharacterEncoding("UTF-8");
			response.setHeader("content-Disposition", "attachment;filename=" + fileName);
			OutputStream os = response.getOutputStream();
			wb.write(os);
			os.flush();
			os.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return new WebJsonBean(CODE.SUCCESS);
	}

}
